<?php

class Menu_controller extends CI_Controller {
    
    function Menu_controller() {
        parent::__construct();
        
        $this->load->model('Menu_model');
    }
    
    public function index($language_id) {
        $this->load->model('Language_model');
        
        $roots = $this->Menu_model->menu_language_root_list($language_id);
        if(!empty($roots)) {
               foreach($roots as $key => $root) {
                    $organized[$key] = $root;
                    //$organized[$key]['leafs'] = $this->Menu_model->sub_menu_list($root['menu_id']);
                    $leafs = $this->Menu_model->sub_menu_list($root['menu_id']);
                    if(!empty($leafs))
                        $organized[$key]['leafs'] = $this->submenusTree($root['menu_id']);
            }
        } else $organized = array();
        //echo"<pre>".print_r($organized,true)."</pre>"; die();
        $data['menus'] = $organized;
        $data['language'] = $this->Language_model->language_get($language_id);
        $data['languages'] = $this->Language_model->languages_select($language_id);
        
        $data['route'] = 'view_menus';
        $this->admin($data);
    }
    
    public function admin($data) {
        $this->load->library('session');
        $data['lang'] = $this->session->userdata('lang');
        $data['member_id'] = $this->session->userdata('member_id');
        $data['firstname'] = $this->session->userdata('firstname');        
        $this->load->model('Panel_model');
        $data['panel'] = $this->Panel_model->admin_menus_list();
        foreach ($data['panel'] as $key => $option) {
            $data['panel'][$key]['option'] = $this->Panel_model->admin_options_list($option['admin_panel_id']);
        }
        
        $this->load->view('layout/admin', $data);
    }
    
    public function set() {
        $post = $this->input->post();
        
        // restaurant customization
        if(empty($post['title'])) $post['title'] = $post['name'];
        if(empty($post['parent']) && empty($post['child'])) {
            $menu_id = $this->Menu_model->menu_root_set($post['language'], $post['name'], $post['title']);
            
            if(!empty($post['union']))
                $this->Menu_model->menu_record_update($menu_id, 'flag', 'bit', '00000001');
        } else {
            if(!empty($post['parent']) && empty($post['child']))
                $this->Menu_model->menu_set($post['parent'], $post['language'], $post['name'], $post['title']);
            else
                $this->Menu_model->menu_set($post['child'], $post['language'], $post['name'], $post['title']);
        }
        
        redirect('/router/view_menus/'.$post['language'],'refresh');
    }
    
    public function update($menu_id) {
        $menu = $this->Menu_model->menu_get($menu_id);
        $this->load->model('Content_model');
        $page = $this->Content_model->content_menu_get($menu_id, $menu['language_id']);
        $post = $this->input->post();
        
        if(empty($page)) {
            if(empty($post['parent']) && empty($post['child'])) {
                $this->Menu_model->menu_root_update($menu_id, $post['language'], $post['name'], $post['title']);
                
                if(!empty($post['union']))
                    $this->Menu_model->menu_record_update($menu_id, 'flag', 'bit', '00000001');
            } else {
                if(!empty($post['parent']) && !empty($post['child'])) {
                    $this->Menu_model->menu_update($menu_id, $post['child'], $post['language'], $post['name'], $post['title']);
                } else {
                    if(!empty($post['parent']) && empty($post['child']))
                        $this->Menu_model->menu_update($menu_id, $post['parent'], $post['language'], $post['name'], $post['title']);
                    else
                        $this->Menu_model->menu_update($menu_id, $post['child'], $post['language'], $post['name'], $post['title']);
                }
            }
        } else {
            $this->Menu_model->menu_root_update($menu_id, $post['language'], $post['name'], $post['name']);
            
            $this->load->library('session');
            $member_id = $this->session->userdata('member_id');
            $this->load->model('Content_model');
            $this->Content_model->content_update($page['content_id'] , $menu_id, $member_id, $post['language'], $post['name'], $post['name'], $post['content']);
        }
        
        $this->index($post['language']);
    }
    
    // sets data needed by edit view and redirects towards admin
    public function edit($menu_id) {
        $menu = $this->Menu_model->menu_get($menu_id);
        $this->load->model('Content_model');
        $page = $this->Content_model->content_menu_get($menu_id, $menu['language_id']);
        
        $this->load->model('Language_model');

        $data['menu_id'] = $menu_id;
        $data['language_id'] = $menu['language_id'];
        $data['language'] = $this->Language_model->language_get($menu['language_id']);
        $data['languages'] = $this->Language_model->languages_select($menu['language_id']);
        $data['name'] = $menu['name'];
        
        if(empty($page)) {
            $data['name'] = $menu['name'];
            $data['title'] = $menu['title'];
            
            $data['menus'] = $this->Menu_model->menu_root_list();
            $data['submenus'] = $this->Menu_model->menu_first_born_child_list();
            
            $data['route'] = 'edit_menu';
        } else {
            $data['content'] = $page['content'];
            
            $data['route'] = 'edit_page_menu';
        }
        
        $this->admin($data);
    }
    private function submenusTree($menu_id) {
        $this->load->model('Menu_model');
        $subMenus = $this->Menu_model->sub_menu_list($menu_id);
        foreach($subMenus as $key => $menu) {
            $subMenus[$key]['submenu'] = $this->submenusTree($menu['menu_id']);
        }
        return $subMenus;
    }
    
    public function delete($menu_id) {
        $menu = $this->Menu_model->menu_get($menu_id);
        
        $this->Menu_model->menu_delete($menu_id);
        
        $this->index($menu['language_id']);
    }
    
    // updates the order of menu to one value up or one value down
    public function order($direction, $menu_id) {
        $menu = $this->Menu_model->menu_get($menu_id);
        if(!is_null($menu['parent_menu_id']))
            $menus = $this->Menu_model->sub_menu_list($menu['parent_menu_id']);
        else
            $menus = $this->Menu_model->menu_root_list();
        switch($direction) {
            case 0 : {
                if($menu['ordering'] > 1) {
                    if(!is_null($menu['parent_menu_id']))
                        $this->Menu_model->menu_order($menu_id, '+', '-');
                    else
                        $this->Menu_model->menu_root_order($menu_id, '+', '-');
                }
            } break;
            case 1 : {
                if($menu['ordering'] < count($menus)) {
                    if(!is_null($menu['parent_menu_id']))
                        $this->Menu_model->menu_order($menu_id, '-', '+');
                    else
                        $this->Menu_model->menu_root_order($menu_id, '-', '+');
                }
            } break;
        }
        
        redirect('/router/view_menus/'.$menu['language_id']);
    }
}
?>
